package org.example.spring.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.example.spring.entity.ChattingList;
import org.example.spring.entity.Groupchatting;
import org.example.spring.entity.User;
import org.example.spring.entity.VO.ListVO;
import org.example.spring.mapper.GroupchattingMapper;
import org.example.spring.mapper.ListMapper;
import org.example.spring.mapper.UserMapper;
import org.example.spring.service.IListService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.spring.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 聊天对照表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-05-01
 */
@Service
public class ListServiceImpl extends ServiceImpl<ListMapper, ChattingList> implements IListService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    GroupchattingMapper groupchattingMapper;

    /**
     * 获得当前用户聊天列表
     * @return
     */
    @Override
    public List<ListVO> getList() {
        //获得当前登录ID
        String jwt = httpServletRequest.getHeader("token");
        Claims claims = JWTUtils.parseJWT(jwt);
        Integer currentId = claims.get("id", Integer.class);
        QueryWrapper queryWrapper=new QueryWrapper<>().eq("user_id",currentId);
        List<ChattingList> chattingList =list(queryWrapper);
        List<ListVO> alllist=new ArrayList<>();
        for ( ChattingList chatting : chattingList) {
            ListVO listVO = new ListVO();
            listVO.setType(chatting.getType());
            listVO.setOtherId(chatting.getOtherId());
            listVO.setNewMsg(chatting.getNewMsg());
            //群聊
            if (chatting.getType()==2){
                Integer groupId=chatting.getOtherId();
                Groupchatting group = groupchattingMapper.selectById(groupId);
                listVO.setImg(group.getImg());
                listVO.setName(group.getName());
                //根据群聊里的ID来找到用户信息
                String[] ids = group.getPersonId().split(",");
                List<User> users=new ArrayList<>();
                for (String sid:ids) {
                    Integer id= Integer.valueOf(sid);
                    User user = userMapper.selectById(id);
                    users.add(user);
                }
                listVO.setPerson(users);

            }else
            //私聊部分
            {
                Integer id=chatting.getOtherId();
                List<User> users=new ArrayList<>();
                User user = userMapper.selectById(id);
                users.add(user);
                listVO.setPerson(users);
                listVO.setName(user.getName());
                listVO.setImg(user.getImg());
            }
            alllist.add(listVO);
            }
        return alllist;
    }
}
